﻿using Microsoft.Win32;
using System;
using System.Drawing;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;


namespace LoRa_Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;//设置该属性 为false
        }

        private float FormHeight;
        private float FormWidth;
        /// 将控件的宽，高，左边距，顶边距和字体大小暂存到tag属性中
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        //根据窗体大小调整控件大小
        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {

                if (con.Tag == null) continue;
                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });//获取控件的Tag属性值，并分割后存储字符串数组
                float a = Convert.ToSingle(mytag[0]) * newx;//根据窗体缩放比例确定控件的值，宽度
                con.Width = (int)a;//宽度
                a = Convert.ToSingle(mytag[1]) * newy;//高度
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;//左边距离
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;//上边缘距离
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * newy;//字体大小
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
            if (keyCom != null)
            {
                string[] sSubKeys = keyCom.GetValueNames();
                LoRaSerial.Items.Clear();

                foreach (string sName in sSubKeys)
                {
                    string sValue = (string)keyCom.GetValue(sName);
                    LoRaSerial.Items.Add(sValue);
                }
                if (LoRaSerial.Items.Count > 0)
                {
                    LoRaSerial.SelectedIndex = 0;
                }
            }
            //LoRa串口
            LoRaBaudRate.Text = "57600";
            LoRaDataBits.Text = "8";
            LoRaStop.Text = "1";
            LoRaParity.Text = "无";

            CheckForIllegalCrossThreadCalls = false;
            FormWidth = this.Width;//获取窗体的宽度
            FormHeight = this.Height;//获取窗体的高度
            setTag(this);//调用方法
        }
        private void Form1_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / FormWidth; //窗体宽度缩放比例
            float newy = (this.Height) / FormHeight;//窗体高度缩放比例
            setControls(newx, newy, this);//随窗体改变控件大小
        }

        //LoRa串口
        private SerialPort LoRa_port = new SerialPort();
        //打开LoRa串口线程
        Thread LoRaPort;
        private void button1_Click(object sender, EventArgs e)
        {
            //打开串口
            LoRaPort = new Thread(OpenLoRa_port);
            LoRaPort.IsBackground = true;
            LoRaPort.Start();
        }
        //打开串口
        private void OpenLoRa_port()
        {
            if (!isOpened)
            {
                LoRa_port.PortName = LoRaSerial.Text;
                LoRa_port.BaudRate = Convert.ToInt32(LoRaBaudRate.Text, 10);
                LoRa_port.DataBits = Convert.ToInt32(LoRaDataBits.Text, 10);
                switch (LoRaStop.Text) //停止位 
                {
                    case "1":
                        LoRa_port.StopBits = StopBits.One;
                        break;
                    case "1.5":
                        LoRa_port.StopBits = StopBits.OnePointFive;
                        break;
                    case "2":
                        LoRa_port.StopBits = StopBits.Two;
                        break;
                    default:
                        MessageBox.Show("Error：参数不正确!", "Error");
                        break;
                }
                switch (LoRaParity.Text) //校验位 
                {
                    case "无":
                        LoRa_port.Parity = Parity.None;
                        break;
                    case "奇校验":
                        LoRa_port.Parity = Parity.Odd;
                        break;
                    case "偶校验":
                        LoRa_port.Parity = Parity.Even;
                        break;
                    default:
                        MessageBox.Show("Error：参数不正确!", "Error");
                        break;
                }
                try
                {
                    LoRa_port.Open();     //打开串口
                    button1.Text = "关闭串口";
                    LoRaSerial.Enabled = false;//关闭使能
                    LoRaBaudRate.Enabled = false;
                    LoRaDataBits.Enabled = false;
                    LoRaStop.Enabled = false;
                    LoRaParity.Enabled = false;
                    isOpened = true;
                    LoRa_port.DataReceived += new SerialDataReceivedEventHandler(GetLoRa_Order);//串口接收处理函数
                }
                catch
                {
                    MessageBox.Show("串口打开失败！");
                }
            }
            else
            {
                try
                {
                    LoRa_port.Close();     //关闭串口
                    button1.Text = "打开串口";
                    LoRaSerial.Enabled = true;//打开使能
                    LoRaBaudRate.Enabled = true;
                    LoRaDataBits.Enabled = true;
                    LoRaStop.Enabled = true;
                    LoRaParity.Enabled = true;
                    isOpened = false;
                }
                catch
                {
                    MessageBox.Show("串口关闭失败！");
                }
            }
        }
        //接收串口数据
        private void GetLoRa_Order(object sender, SerialDataReceivedEventArgs e)
        {
            while (true)
            {
                if (LoRa_port.IsOpen)
                {
                    int count = LoRa_port.BytesToRead;
                    if (count >= 1)
                    {
                        byte[] readBuffer = new byte[count];
                        try
                        {
                            LoRa_port.Read(readBuffer, 0, count);
                            string DTUReceive = Encoding.UTF8.GetString(readBuffer);
                            if (count != 0)
                            {
                                richTextBox1.AppendText(DTUReceive);
                                richTextBox1.Focus();   //让光标到这来
                                richTextBox1.Select(richTextBox1.TextLength, 0);
                                this.richTextBox1.ScrollToCaret(); //设置光标到最后
                            }

                        }
                        catch (TimeoutException) { }
                    }
                }
            }
        }

        bool isOpened = false;//串口连接标志
        int[] Order = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        private void SendOrder(object sender, EventArgs e)
        {
            string result = string.Join(",", Order);
            textBox00 = (result + "\r \n");
            //LoRa_port.Write(result + "\r \n");

        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            string result = string.Join(",", Order);
            textBox00 = (result + "\r\n");
            //LoRa_port.Write(result + "\r \n");
        }

        string textBox00 = "";
        //初值化0
        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[0] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true; 
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button2_MouseUp(object sender, MouseEventArgs e)
        {
            Order[0] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //垂向复位1
        private void button3_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[1] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button3_MouseUp(object sender, MouseEventArgs e)
        {
            Order[1] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //纵向复位2
        private void button4_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[2] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button4_MouseUp(object sender, MouseEventArgs e)
        {
            Order[2] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //加速前进3
        private void botton6_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[3] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button6_MouseUp(object sender, MouseEventArgs e)
        {
            Order[3] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //减速前进4
        private void button7_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[4] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button7_MouseUp(object sender, MouseEventArgs e)
        {
            Order[4] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //加速后退5
        private void button8_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[5] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button8_MouseUp(object sender, MouseEventArgs e)
        {
            Order[5] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //减速后退6
        private void button9_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[6] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button9_MouseUp(object sender, MouseEventArgs e)
        {
            Order[6] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //加速上浮7
        private void button10_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[7] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button10_MouseUp(object sender, MouseEventArgs e)
        {
            Order[7] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //减速上浮8
        private void button11_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[8] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button11_MouseUp(object sender, MouseEventArgs e)
        {
            Order[8] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //加速下潜9
        private void button12_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[9] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e); 
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button12_MouseUp(object sender, MouseEventArgs e)
        {
            Order[9] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //减速下潜10
        private void button13_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[10] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button13_MouseUp(object sender, MouseEventArgs e)
        {
            Order[10] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //前翻11
        private void button14_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[11] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button14_MouseUp(object sender, MouseEventArgs e)
        {
            Order[11] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //后翻12
        private void button15_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[12] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button15_MouseUp(object sender, MouseEventArgs e)
        {
            Order[12] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //左翻13
        private void button16_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[13] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button16_MouseUp(object sender, MouseEventArgs e)
        {
            Order[13] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //右翻14
        private void button17_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[14] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                // SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button17_MouseUp(object sender, MouseEventArgs e)
        {
            Order[14] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //左前转15
        private void button18_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[15] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                //SendOrder(sender, e);
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button18_MouseUp(object sender, MouseEventArgs e)
        {
            Order[15] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }
        //右前转16
        private void button19_MouseDown(object sender, MouseEventArgs e)
        {
            if (isOpened)
            {
                Order[16] = 1;
                timer1.Interval = int.Parse(textBox1.Text);
                timer1.Enabled = true;
                timer2.Interval = int.Parse(textBox1.Text);
                timer2.Enabled = true;
                //SendOrder(sender, e);                
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }
        private void button19_MouseUp(object sender, MouseEventArgs e)
        {
            Order[16] = 0;
            SendOrder(sender, e);
            timer1.Enabled = false;
            timer2.Enabled = false;
        }

        private void button21_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
        }

        /// <summary>
        /// 接受textbox00部分，并发送指令
        /// </summary>
        /// 

        private void SPortUnOpen()
        {
            MessageBox.Show("UNO串口未打开！");
        }

        int v = 1;
        int U1_0, U2_0, U3_0, U4_0, L_0, R_0;
        int reset_Y_1, reset_Y_2, reset_Z_1, reset_Z_2, reset_Z_3, reset_Z_4;

        private void AllCtrl()
        {
            string[] AllOrder = (textBox00).Split(',');
            if (AllOrder.Length == 17)
            {
                try
                {
                    //初始化
                    if (int.Parse(AllOrder[0]) == 1)
                    {
                        AllReset();
                        
                    }
                    //垂向复位
                    if (int.Parse(AllOrder[1]) == 1)
                    {
                        ResetZ();
                        
                    }
                    //纵向复位
                    if (int.Parse(AllOrder[2]) == 1)
                    {
                        ResetY();
                        
                    }
                    //加速前进
                    if (int.Parse(AllOrder[3]) == 1)
                    {
                        AddForward();
                        
                    }
                    //减速前进
                    if (int.Parse(AllOrder[4]) == 1)
                    {
                        SubForward();
                        
                    }
                    //加速后退
                    if (int.Parse(AllOrder[5]) == 1)
                    {
                        AddBack();
                        
                    }
                    //减速后退
                    if (int.Parse(AllOrder[6]) == 1)
                    {
                        SubBack();
                        
                    }
                    //加速上浮
                    if (int.Parse(AllOrder[7]) == 1)
                    {
                        AddFloat();
                        
                    }
                    //减速上浮
                    if (int.Parse(AllOrder[8]) == 1)
                    {
                        SubFloat();
                    }
                    //加速下潜
                    if (int.Parse(AllOrder[9]) == 1)
                    {
                        AddDive();
                        
                    }
                    //减速下潜
                    if (int.Parse(AllOrder[10]) == 1)
                    {
                        SubDive();
                        
                    }
                    //前翻
                    if (int.Parse(AllOrder[11]) == 1)
                    {
                        FlipForward();
                        
                    }
                    //后翻
                    if (int.Parse(AllOrder[12]) == 1)
                    {
                        FlipBack();
                        
                    }
                    //左翻
                    if (int.Parse(AllOrder[13]) == 1)
                    {
                        FlipLeft();
                        
                    }
                    //右翻
                    if (int.Parse(AllOrder[14]) == 1)
                    {
                        FlipRight();
                        
                    }
                    //左转
                    if (int.Parse(AllOrder[15]) == 1)
                    {
                        TurnLeft();
                        
                    }
                    //右转
                    if (int.Parse(AllOrder[16]) == 1)
                    {
                        TurnRight();
                        
                    }
                }
                catch { }
            }

        }
        private void AllReset()
        {
            if (LoRa_port.IsOpen)
            {
                //发送数据          
                string Stopdata = 1500 + "," + 1500 + "," + 1500 + "," + 1500 + "," + 1508 + "," + 1488 + ",";
                //发送停转数据
                LoRa_port.Write(Stopdata);//写数据        
                Sendbox.Text = Stopdata;

            }
            else
            {
                SPortUnOpen();
            }
        }
        //垂直复位事件1
        private void ResetZ()
        {
            if (LoRa_port.IsOpen)
            {
                if (Sendbox.Text == "")
                {
                    AllReset();
                }
                else
                {
                    string[] Z_Data = Sendbox.Text.Split(',');
                    reset_Y_1 = Int32.Parse(Z_Data[4]);
                    reset_Y_2 = Int32.Parse(Z_Data[5]);
                    //定义停转数据
                    string Stopdata = 1500 + "," + 1500 + "," + 1500 + "," + 1500 + "," + reset_Y_1 + "," + reset_Y_2 + ",";
                    //发送停转数据
                    LoRa_port.Write(Stopdata);//写数据      

                    Sendbox.Text = Stopdata;
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //纵向复位事件2
        private void ResetY()
        {
            if (LoRa_port.IsOpen)
            {
                if (Sendbox.Text == "")
                {
                    AllReset();
                }
                else
                {
                    string[] Y_Data = Sendbox.Text.Split(',');
                    reset_Z_1 = Int32.Parse(Y_Data[0]);
                    reset_Z_2 = Int32.Parse(Y_Data[1]);
                    reset_Z_3 = Int32.Parse(Y_Data[2]);
                    reset_Z_4 = Int32.Parse(Y_Data[3]);
                    //定义停转数据
                    string Stopdata = reset_Z_1 + "," + reset_Z_2 + "," + reset_Z_3 + "," + reset_Z_4 + "," + 1508 + "," + 1488 + ",";
                    //发送停转数据
                    LoRa_port.Write(Stopdata);//写数据        
                    Sendbox.Text = Stopdata;
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //加速前进事件3
        private void AddForward()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string ForwardAdd = Sendbox.Text;
                string[] ForwardAddData = ForwardAdd.Split(',');
                U1_0 = int.Parse(ForwardAddData[0]);
                U2_0 = int.Parse(ForwardAddData[1]);
                U3_0 = int.Parse(ForwardAddData[2]);
                U4_0 = int.Parse(ForwardAddData[3]);
                L_0 = int.Parse(ForwardAddData[4]);
                R_0 = int.Parse(ForwardAddData[5]);
                if ((L_0 >= 1508) && (L_0 < 1900) && (R_0 >= 1488) && (R_0 < 1900))
                {
                    L_0 = L_0 + 1 * v;
                    R_0 = R_0 + 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
                else if ((L_0 == 1900) && (R_0 == 1900))
                {
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + 1900 + "," + 1900 + ",";
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }

                //前进复位
                else if ((L_0 < 1508) || (R_0 < 1488))
                {
                    ResetY();
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //减速前进事件4
        private void SubForward()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string SubForward = Sendbox.Text;
                string[] SubForwardData = SubForward.Split(',');
                U1_0 = int.Parse(SubForwardData[0]);
                U2_0 = int.Parse(SubForwardData[1]);
                U3_0 = int.Parse(SubForwardData[2]);
                U4_0 = int.Parse(SubForwardData[3]);
                L_0 = int.Parse(SubForwardData[4]);
                R_0 = int.Parse(SubForwardData[5]);
                if ((L_0 <= 1900) && (R_0 <= 1900) && (L_0 > 1508) && (R_0 > 1488))
                {
                    L_0 = L_0 - 1 * v;
                    R_0 = R_0 - 1 * v;
                    //接收框显示数值
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
                else if ((L_0 == 1508) && (R_0 == 1488))
                {
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + 1508 + "," + 1488 + ",";
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
                else if ((L_0 < 1488) || (R_0 < 1488))
                {
                    ResetY();
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //加速后退事件5
        private void AddBack()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string AddBack = Sendbox.Text;
                string[] AddBackData = AddBack.Split(',');
                U1_0 = int.Parse(AddBackData[0]);
                U2_0 = int.Parse(AddBackData[1]);
                U3_0 = int.Parse(AddBackData[2]);
                U4_0 = int.Parse(AddBackData[3]);
                L_0 = int.Parse(AddBackData[4]);
                R_0 = int.Parse(AddBackData[5]);
                if ((L_0 <= 1508) && (R_0 <= 1488) && (L_0 > 1076) && (R_0 > 1076))
                {
                    L_0 = L_0 - 1 * v;
                    R_0 = R_0 - 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);
                }
                else if ((L_0 == 1076) && (R_0 == 1076))
                {
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + 1076 + "," + 1076 + ",";
                    LoRa_port.Write(Sendbox.Text);
                }
                if ((L_0 > 1508) || (R_0 > 1488))
                {
                    ResetY();
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //减速后退事件6
        private void SubBack()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string SubBack = Sendbox.Text;
                string[] SubBackData = SubBack.Split(',');
                U1_0 = int.Parse(SubBackData[0]);
                U2_0 = int.Parse(SubBackData[1]);
                U3_0 = int.Parse(SubBackData[2]);
                U4_0 = int.Parse(SubBackData[3]);
                L_0 = int.Parse(SubBackData[4]);
                R_0 = int.Parse(SubBackData[5]);
                if ((L_0 >= 1076) && (R_0 >= 1076) && (L_0 < 1508) && (R_0 < 1488))
                {
                    L_0 = L_0 + 1 * v;
                    R_0 = R_0 + 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);
                }
                else if ((L_0 >= 1508) || (R_0 >= 1488))
                {
                    ResetY();
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //加速上浮事件7
        private void AddFloat()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string AddFloat = Sendbox.Text;
                string[] AddFloatData = AddFloat.Split(',');
                U1_0 = int.Parse(AddFloatData[0]);
                U2_0 = int.Parse(AddFloatData[1]);
                U3_0 = int.Parse(AddFloatData[2]);
                U4_0 = int.Parse(AddFloatData[3]);
                L_0 = int.Parse(AddFloatData[4]);
                R_0 = int.Parse(AddFloatData[5]);
                if ((U1_0 <= 1500) && (U2_0 <= 1500) && (U3_0 <= 1500) && (U4_0 <= 1500) &&
                    (U1_0 > 1100) && (U2_0 > 1100) && (U3_0 > 1100) && (U4_0 > 1100))
                {
                    U1_0 = U1_0 - 1 * v;
                    U2_0 = U2_0 - 1 * v;
                    U3_0 = U3_0 - 1 * v;
                    U4_0 = U4_0 - 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";//接收框显示数值

                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
                //垂向复位
                if (((U1_0 > 1500) || (U2_0 > 1500) || (U3_0 > 1500) || (U4_0 > 1500)))
                //发送复位数据
                {
                    ResetZ();
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //减速上浮事件8
        private void SubFloat()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string SubFloat = Sendbox.Text;
                string[] SubFloatData = SubFloat.Split(',');
                U1_0 = int.Parse(SubFloatData[0]);
                U2_0 = int.Parse(SubFloatData[1]);
                U3_0 = int.Parse(SubFloatData[2]);
                U4_0 = int.Parse(SubFloatData[3]);
                L_0 = int.Parse(SubFloatData[4]);
                R_0 = int.Parse(SubFloatData[5]);

                if ((U1_0 < 1500) && (U2_0 < 1500) && (U3_0 < 1500) && (U4_0 < 1500) &&
                    (U1_0 >= 1100) && (U2_0 >= 1100) && (U3_0 >= 1100) && (U4_0 >= 1100))
                {
                    U1_0 = U1_0 + 1 * v;
                    U2_0 = U2_0 + 1 * v;
                    U3_0 = U3_0 + 1 * v;
                    U4_0 = U4_0 + 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";//接收框显示数值
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
                //垂向复位
                if ((U1_0 >= 1500) || (U2_0 >= 1500) || (U3_0 >= 1500) || (U4_0 >= 1500))
                {
                    ResetZ();
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //加速下潜事件9
        private void AddDive()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string AddDive = Sendbox.Text;
                string[] AddDiveData = AddDive.Split(',');
                U1_0 = int.Parse(AddDiveData[0]);
                U2_0 = int.Parse(AddDiveData[1]);
                U3_0 = int.Parse(AddDiveData[2]);
                U4_0 = int.Parse(AddDiveData[3]);
                L_0 = int.Parse(AddDiveData[4]);
                R_0 = int.Parse(AddDiveData[5]);

                if ((U1_0 >= 1500) && (U2_0 >= 1500) && (U3_0 >= 1500) && (U4_0 >= 1500) &&
                    (U1_0 < 1900) && (U2_0 < 1900) && (U3_0 < 1900) && (U4_0 < 1900))
                {
                    U1_0 = U1_0 + 1 * v;
                    U2_0 = U2_0 + 1 * v;
                    U3_0 = U3_0 + 1 * v;
                    U4_0 = U4_0 + 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";//接收框显示数值
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
                //发送复位数据
                if ((U1_0 < 1500) || (U2_0 < 1500) || (U3_0 < 1500) || (U4_0 < 1500))
                {
                    ResetZ();
                }
                else if ((U1_0 == 1900) && (U2_0 == 1900) && (U3_0 == 1900) && (U4_0 == 1900))
                {
                    Sendbox.Text = 1900 + "," + 1900 + "," + 1900 + "," + 1900 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口

                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //减速下潜事件10
        private void SubDive()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string SubDive = Sendbox.Text;
                string[] SubDiveData = SubDive.Split(',');
                U1_0 = int.Parse(SubDiveData[0]);
                U2_0 = int.Parse(SubDiveData[1]);
                U3_0 = int.Parse(SubDiveData[2]);
                U4_0 = int.Parse(SubDiveData[3]);
                L_0 = int.Parse(SubDiveData[4]);
                R_0 = int.Parse(SubDiveData[5]);

                if ((U1_0 <= 1900) && (U2_0 <= 1900) && (U3_0 <= 1900) && (U4_0 <= 1900) &&
                    (U1_0 > 1500) && (U2_0 > 1500) && (U3_0 > 1500) && (U4_0 > 1500))
                {
                    U1_0 = U1_0 - 1 * v;
                    U2_0 = U2_0 - 1 * v;
                    U3_0 = U3_0 - 1 * v;
                    U4_0 = U4_0 - 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
                //垂向复位
                if ((U1_0 <= 1500) || (U2_0 <= 1500) || (U3_0 <= 1500) || (U4_0 <= 1500))
                {
                    ResetZ();
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //前翻事件11
        private void FlipForward()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string FlipForward = Sendbox.Text;
                string[] FlipForwardData = FlipForward.Split(',');
                U1_0 = int.Parse(FlipForwardData[0]);
                U2_0 = int.Parse(FlipForwardData[1]);
                U3_0 = int.Parse(FlipForwardData[2]);
                U4_0 = int.Parse(FlipForwardData[3]);
                L_0 = int.Parse(FlipForwardData[4]);
                R_0 = int.Parse(FlipForwardData[5]);

                if ((U1_0 >= 1500) && (U2_0 >= 1500) && (U1_0 < 1900) && (U2_0 < 1900) &&
                    (U3_0 <= 1500) && (U4_0 <= 1500) && (U3_0 > 1100) && (U4_0 > 1100))
                {
                    U1_0 = U1_0 + 1 * v;
                    U2_0 = U2_0 + 1 * v;
                    U3_0 = U3_0 - 1 * v;
                    U4_0 = U4_0 - 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";//接收框显示数值
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
                if ((U1_0 < 1500) || (U2_0 < 1500) || (U3_0 > 1500) || (U4_0 > 1500))
                //发送复位数据
                {
                    ResetZ();
                }
                if (((U1_0 == 1900) && (U2_0 == 1900)) || ((U3_0 == 1100) && (U4_0 == 1100)))
                {
                    Sendbox.Text = 1900 + "," + 1900 + "," + 1100 + "," + 1100 + "," + L_0 + "," + R_0 + ",";//接收框显示数值
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //后翻事件12
        private void FlipBack()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string FlipBack = Sendbox.Text;
                string[] FlipBackData = FlipBack.Split(',');
                U1_0 = int.Parse(FlipBackData[0]);
                U2_0 = int.Parse(FlipBackData[1]);
                U3_0 = int.Parse(FlipBackData[2]);
                U4_0 = int.Parse(FlipBackData[3]);
                L_0 = int.Parse(FlipBackData[4]);
                R_0 = int.Parse(FlipBackData[5]);

                if ((U1_0 > 1100) && (U2_0 > 1100) && (U1_0 <= 1500) && (U2_0 <= 1500) &&
                    (U3_0 >= 1500) && (U4_0 >= 1500) && (U3_0 < 1900) && (U4_0 < 1900))
                {
                    U1_0 = U1_0 - 1 * v;
                    U2_0 = U2_0 - 1 * v;
                    U3_0 = U3_0 + 1 * v;
                    U4_0 = U4_0 + 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";//接收框显示数值
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
                if ((U1_0 > 1500) || (U2_0 > 1500) || (U3_0 < 1500) || (U4_0 < 1500))
                //发送复位数据
                {
                    ResetZ();
                }
                if (((U1_0 == 1900) && (U2_0 == 1900)) || ((U3_0 == 1100) && (U4_0 == 1100)))
                {
                    Sendbox.Text = 1900 + "," + 1900 + "," + 1100 + "," + 1100 + "," + L_0 + "," + R_0 + ",";//接收框显示数值
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //左翻事件13
        private void FlipLeft()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string FlipLeft = Sendbox.Text;
                string[] FlipLeftData = FlipLeft.Split(',');
                U1_0 = int.Parse(FlipLeftData[0]);
                U2_0 = int.Parse(FlipLeftData[1]);
                U3_0 = int.Parse(FlipLeftData[2]);
                U4_0 = int.Parse(FlipLeftData[3]);
                L_0 = int.Parse(FlipLeftData[4]);
                R_0 = int.Parse(FlipLeftData[5]);
                if ((U1_0 >= 1500) && (U3_0 >= 1500) && (U1_0 < 1900) && (U3_0 < 1900) &&
                    (U2_0 <= 1500) && (U4_0 <= 1500) && (U2_0 > 1100) && (U4_0 > 1100))
                {
                    U1_0 = U1_0 + 1 * v;
                    U3_0 = U3_0 + 1 * v;
                    U2_0 = U2_0 - 1 * v;
                    U4_0 = U4_0 - 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
                if ((U1_0 < 1500) || (U3_0 < 1500) || (U2_0 > 1500) || (U4_0 > 1500))
                //发送复位数据
                {
                    ResetZ();
                }
                if (((U1_0 == 1900) && (U3_0 == 1900)) || ((U2_0 == 1100) && (U4_0 == 1100)))
                {
                    Sendbox.Text = 1900 + "," + 1100 + "," + 1900 + "," + 1100 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //右翻事件14
        private void FlipRight()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string FlipRight = Sendbox.Text;
                string[] FlipRightData = FlipRight.Split(',');
                U1_0 = int.Parse(FlipRightData[0]);
                U2_0 = int.Parse(FlipRightData[1]);
                U3_0 = int.Parse(FlipRightData[2]);
                U4_0 = int.Parse(FlipRightData[3]);
                L_0 = int.Parse(FlipRightData[4]);
                R_0 = int.Parse(FlipRightData[5]);
                if ((U1_0 <= 1500) && (U3_0 <= 1500) && (U1_0 > 1100) && (U3_0 > 1100) &&
                    (U2_0 >= 1500) && (U4_0 >= 1500) && (U2_0 < 1900) && (U4_0 < 1900))
                {
                    U1_0 = U1_0 - 1 * v;
                    U3_0 = U3_0 - 1 * v;
                    U2_0 = U2_0 + 1 * v;
                    U4_0 = U4_0 + 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
                if ((U1_0 > 1500) || (U3_0 > 1500) || (U2_0 < 1500) || (U4_0 < 1500))
                //发送复位数据
                {
                    ResetZ();
                }
                if (((U1_0 == 1100) && (U3_0 == 1100)) || ((U2_0 == 1900) && (U4_0 == 1900)))
                {
                    Sendbox.Text = 1100 + "," + 1900 + "," + 1100 + "," + 1900 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);//将字符串写入串口
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //左转事件15
        private void TurnLeft()
        {
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string TurnLeft = Sendbox.Text;
                string[] TurnLeftData = TurnLeft.Split(',');
                U1_0 = int.Parse(TurnLeftData[0]);
                U2_0 = int.Parse(TurnLeftData[1]);
                U3_0 = int.Parse(TurnLeftData[2]);
                U4_0 = int.Parse(TurnLeftData[3]);
                L_0 = int.Parse(TurnLeftData[4]);
                R_0 = int.Parse(TurnLeftData[5]);

                if ((L_0 > 1076) && (L_0 <= 1508) && (R_0 >= 1488) && (R_0 < 1900))
                {
                    L_0 = L_0 - 1 * v;
                    R_0 = R_0 + 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);
                }
                else if ((L_0 == 1076) && (R_0 == 1900))
                {
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + 1900 + "," + 1076 + ",";
                    LoRa_port.Write(Sendbox.Text);
                }
                //纵向复位
                else if ((L_0 > 1508) || (R_0 < 1488))
                {
                    ResetY();
                }
            }
            else
            {
                SPortUnOpen();
            }
        }
        //右转事件16
        private void TurnRight()
        {
            // 复位
            if (Sendbox.Text == "")
            {
                AllReset();
            }
            if (LoRa_port.IsOpen)
            {
                string TurnRight = Sendbox.Text;
                string[] TurnRightData = TurnRight.Split(',');
                U1_0 = int.Parse(TurnRightData[0]);
                U2_0 = int.Parse(TurnRightData[1]);
                U3_0 = int.Parse(TurnRightData[2]);
                U4_0 = int.Parse(TurnRightData[3]);
                L_0 = int.Parse(TurnRightData[4]);
                R_0 = int.Parse(TurnRightData[5]);

                if ((L_0 >= 1508) && (L_0 < 1900) && (R_0 > 1076) && (R_0 <= 1488))
                {
                    L_0 = L_0 + 1 * v;
                    R_0 = R_0 - 1 * v;
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + L_0 + "," + R_0 + ",";
                    LoRa_port.Write(Sendbox.Text);
                }

                else if ((L_0 == 1900) && (R_0 == 1076))
                {
                    Sendbox.Text = U1_0 + "," + U2_0 + "," + U3_0 + "," + U4_0 + "," + 1900 + "," + 1076 + ",";
                    LoRa_port.Write(Sendbox.Text);
                }
                //纵向复位
                else if ((L_0 < 1508) || (R_0 > 1488))
                {
                    ResetY();
                }
            }
            else
            {
                SPortUnOpen();
            }
        }

        //集合所有按键事件的计时器响应
        private void timer2_Tick(object sender, EventArgs e)
        {
            //初始化
            if (Order[0] == 1)
            {
                AllReset();
                
            }
            //垂向复位
            if (Order[1] == 1)
            {
                ResetZ();
                
            }
            //纵向复位
            if (Order[2] == 1)
            {
                ResetY();
                
            }
            //加速前进
            if (Order[3] == 1)
            {
                AddForward();
                
            }
            //减速前进
            if (Order[4] == 1)
            {
                SubForward();
                
            }
            //加速后退
            if (Order[5] == 1)
            {
                AddBack();
                
            }
            //减速后退
            if (Order[6] == 1)
            {
                SubBack();
                
            }
            //加速上浮
            if (Order[7] == 1)
            {
                AddFloat();
                
            }
            //减速上浮
            if (Order[8] == 1)
            {
                SubFloat();
                
            }
            //加速下潜
            if (Order[9] == 1)
            {
                AddDive();
                
            }
            //减速下潜
            if (Order[10] == 1)
            {
                SubDive();
                
            }
            //前翻
            if (Order[11] == 1)
            {
                FlipForward();
                
            }
            //后翻
            if (Order[12] == 1)
            {
                FlipBack();
                
            }
            //左翻
            if (Order[13] == 1)
            {
                FlipLeft();
                
            }
            //右翻
            if (Order[14] == 1)
            {
                FlipRight();
                
            }
            //左转
            if (Order[15] == 1)
            {
                TurnLeft();
                
            }
            //右转
            if (Order[16] == 1)
            {
                TurnRight();
                
            }
        }
    }
}
